#ifndef __THROWERR_H__
#define __THROWERR_H__

#include <unistd.h>
#include <cstring>

static char errstr[256];

#define THROW(MSG) { \
   	sprintf(errstr, "(%d) %s (file: '%s' line: %d)", getpid(), MSG, __FILE__, __LINE__); \
   	throw errstr;\
}

#define THROWSTR(MSG, STR) { \
    sprintf(errstr, "(%d) %s: %s (file: '%s' line: %d)", getpid(), MSG, STR, __FILE__, __LINE__); \
    throw errstr;\
}

#define THROWINT(MSG, INT) { \
    sprintf(errstr, "(%d) %s: %d (file: '%s' line: %d)", getpid(), MSG, INT, __FILE__, __LINE__); \
    throw errstr;\
}
	
#define THROW2INT(MSG, INT1, INT2) { \
    sprintf(errstr, "(%d) %s: %d, %d (file: '%s' line: %d)", getpid(), MSG, INT1, INT2, __FILE__, __LINE__); \
    throw errstr;\
}

#define THROW3INT(MSG, INT1, INT2, INT3) { \
    sprintf(errstr, "(%d) %s: %d, %d, %d (file: '%s' line: %d)", getpid(), MSG, INT1, INT2, INT3, __FILE__, __LINE__); \
    throw errstr;\
}

#define THROWDBL(MSG, DBL) { \
    sprintf(errstr, "(%d) %s: %f (file: '%s' line: %d)", getpid(), MSG, DBL, __FILE__, __LINE__); \
    throw errstr;\
}
	
#define THROW2DBL(MSG, DBL1, DBL2) { \
    sprintf(errstr, "(%d) %s: %f, %f (file: '%s' line: %d)", getpid(), MSG, DBL1, DBL2, __FILE__, __LINE__); \
    throw errstr;\
}

#define THROW3DBL(MSG, DBL1, DBL2, DBL3) { \
    sprintf(errstr, "(%d) %s: %f, %f, %f (file: '%s' line: %d)", getpid(), MSG, DBL1, DBL2, DBL3, __FILE__, __LINE__); \
    throw errstr;\
}

#define CHECKERR(CHK, STR) if(CHK)THROW(STR);

#ifdef OMD_DEBUG
#define DEBUGBLOCK 
#define DEBUG(STR) fprintf(stderr,"(%d) %s\n", getpid(), STR)
#define DEBUGSTR(STR, STR1) fprintf(stderr,"(%d) %s: %s\n", getpid(), STR, STR1)
#define DEBUGINT(STR, INT)  fprintf(stderr,"(%d) %s: %d\n", getpid(), STR, INT)
#define DEBUG2INT(STR, INT1, INT2)  fprintf(stderr,"(%d) %s: %d, %d\n", getpid(), STR, INT1, INT2)
#define DEBUG3INT(STR, INT1, INT2, INT3)  fprintf(stderr,"(%d) %s: %d, %d, %d\n", getpid(), STR, INT1, INT2, INT3)
#define DEBUGDBL(STR, DBL)  fprintf(stderr,"(%d) %s: %e\n", getpid(), STR, DBL)
#define DEBUG2DBL(STR, DBL1, DBL2)  fprintf(stderr,"(%d) %s: %e %e\n", getpid(), STR, DBL1, DBL2)
#define DEBUG3DBL(STR, DBL1, DBL2, DBL3)  fprintf(stderr,"(%d) %s: %e %e %e\n", getpid(), STR, DBL1, DBL2, DBL3)
#define DEBUGEXIT {fprintf(stderr,"(%d) Debuging exit point (file: '%s' line: %d)\n", getpid(), __FILE__, __LINE__); exit(1);}
#define WARNING(STR) fprintf(stderr, "(%d) WARNING! %s (file: '%s' line: %d)\n", getpid(), STR, __FILE__, __LINE__)
#else
#define DEBUGBLOCK 
#define DEBUG(STR)
#define DEBUGSTR(STR, STR1)
#define DEBUGINT(STR, INT)
#define DEBUG2INT(STR, INT1, INT2)
#define DEBUG3INT(STR, INT1, INT2, INT3)
#define DEBUGDBL(STR, DBL)
#define DEBUG2DBL(STR, DBL1, DBL2)
#define DEBUG3DBL(STR, DBL1, DBL2, DBL3)
#define DEBUGEXIT
#define WARNING(STR)
#endif

#endif
